package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Menu_User;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Set;

@Service(interfaceClass = MenuService.class)
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Menu> menus = menuDao.findAllMenu(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(menus.getTotal(), menus.getResult());
        return pageResult;
    }

    @Override
    public List<Menu> findAll() {
        List<Menu> menus = menuDao.findAll();
        return menus;
    }

    @Override
    public Result insertMenu(Menu menu) {
        Result access = isAccess(menu);
        if (!access.isFlag()) {
            return access;
        }
        menuDao.insertMenu(menu);
        return new Result(true, "添加餐单成功");
    }

    @Override
    public List<Menu> findAllMenu() {
        return menuDao.findAllId();
    }

    @Override
    public Menu findMenuById(Integer id) {
        Menu menu = menuDao.findMenuById(id);
        return menu;
    }

    @Override
    public Result editMenu(Menu menu) {
        List<Menu> menuList = findAll();
        Menu _menu = menuDao.findMenuById(menu.getId());
        //访问路径是否为空
        Boolean linkUrlFlag = isOnly(menuList, menu.getLinkUrl(), "linkUrl");
        if (!linkUrlFlag) {
            if (_menu.getLinkUrl().equals(menu.getLinkUrl())) {
            } else {
                return new Result(false, "访问路径不能为空或者已存在!");
            }
        }

        //餐单名称是否唯一
        Boolean nameFlag = isOnly(menuList, menu.getName(), "name");
        if (!nameFlag) {
            if (_menu.getName().equals(menu.getName())) {
            } else {
                return new Result(false, "不能为空或餐单已存在!");
            }
        }
        //path是否唯一
        Boolean pathFlag = isOnly(menuList, menu.getPath(), "path");
        if (!pathFlag) {
            if (_menu.getPath().equals(menu.getPath())) {
            } else {
                return new Result(false, "不能为空或餐单路径已存在!");
            }
        }
        //自关联id是否与等级匹配
        Integer parentMenuId = menu.getParentMenuId();
        Integer level = menu.getLevel();
        Menu parentMenu = menuDao.findParentMenuByParentId(parentMenuId);
        if (parentMenuId == null || parentMenu == null || level == null) {
            return new Result(false, "自关联Id和餐单级别不能为空!");
        }
        if ((level - 1) != parentMenu.getLevel()) {
            return new Result(false, "自关联Id和餐单级别等级不匹配!");
        }
        menuDao.updateMenu(menu);
        return new Result(true, "编辑餐单成功!");
    }

    @Override
    public Result deleteMenu(Integer id) {
        //判断menu是否存在子菜单
        Menu menu = menuDao.findMenuById(id);
        List<Menu> menuChildren = menu.getChildren();
        if (menuChildren != null && menuChildren.size() > 0) {
            return new Result(false, "删除餐单存在关联餐单,删除失败!");
        }
        //判断role和menu是否有关联
        Set<Role> roles = menu.getRoles();
        if (roles != null && roles.size() > 0) {
            return new Result(false, "删除餐单存在在角色,删除失败!");
        }
        menuDao.deleteMenuById(id);
        return new Result(true, "删除餐单成功!");
    }

    @Override
    public List<Menu_User> findMenu_UserByUsername(String username) {

        return menuDao.findMenu_UserByUsername(username);
    }


    //判断条件抽取方法
    public Result isAccess(Menu menu) {
        List<Menu> menuList = findAll();
        //访问路径是否为空
        Boolean linkUrlFlag = isOnly(menuList, menu.getLinkUrl(), "linkUrl");
        if (!linkUrlFlag) {
            return new Result(false, "访问路径不能为空或者已存在!");
        }

        //餐单名称是否唯一
        Boolean nameFlag = isOnly(menuList, menu.getName(), "name");
        if (!nameFlag) {
            return new Result(false, "餐单名称不能为空或已存在!");
        }
        //path是否唯一
        Boolean pathFlag = isOnly(menuList, menu.getPath(), "path");
        if (!pathFlag) {
            return new Result(false, "餐单路径或不能为空已存在!");
        }
        //自关联id是否与等级匹配
        Integer parentMenuId = menu.getParentMenuId();
        Integer level = menu.getLevel();
        Menu parentMenu = menuDao.findParentMenuByParentId(parentMenuId);
        if (parentMenuId == null || parentMenu == null || level == null) {
            return new Result(false, "自关联Id和餐单级别不能为空!");
        }
        if ((level - 1) != parentMenu.getLevel()) {
            return new Result(false, "自关联Id和餐单级别等级不匹配!");
        }
        return new Result(true, null);
    }

    //判断是否唯一方法
    public Boolean isOnly(List<Menu> menus, String condition, String paramCondition) {
        if (condition != null && condition.length() > 0) {
            if (paramCondition.equals("name")) {
                for (Menu menu : menus) {
                    if (menu.getName().equals(condition)) {
                        return false;
                    }
                }
                return true;
            } else if (paramCondition.equals("path")) {
                for (Menu menu : menus) {
                    if (menu.getPath().equals(condition)) {
                        return false;
                    }
                }
                return true;
            } else if (paramCondition.equals("linkUrl")) {
                for (Menu menu : menus) {
                    if (menu.getLinkUrl() == null || menu.getLinkUrl().length() == 0) {
                        continue;
                    }
                    if (menu.getLinkUrl().equals(condition)) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        }
        return false;
    }
}
